using UnityEngine;
using QFramework;
using Unity.VisualScripting;

namespace XCZProject
{
	public partial class GetAllExp : GameplayObject
	{
		void Start()
		{
			// Code Here
		}
		private void OnTriggerEnter2D(Collider2D collider2D)
		{
			// 当碰撞的物体拥有“拾取范围”脚本时，则证明经验球碰到了玩家，增加经验并且删除该物体
			if (collider2D.GetComponent<CollectableArea>())
			{
				// 获取场景内所有的经验球
				var exps=FindObjectsByType<Exp>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
				//print("一共发现敌人数量："+enemys.Length);
				foreach (var exp in exps)
				{
					ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							// 获取方向
							var direction = player.Position() - exp.Position();
							exp.transform.Translate(direction.normalized *Time.deltaTime*5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(exp);
				}
				
				
				// 获取场景内所有的金币
				var coins=FindObjectsByType<Coin>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
				//print("一共发现敌人数量："+enemys.Length);
				foreach (var coin in coins)
				{
					ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							// 获取方向
							var direction = player.Position() - coin.Position();
							coin.transform.Translate(direction.normalized *Time.deltaTime*5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(coin);
				}

				AudioKit.PlaySound("get_all_exp");
				this.DestroyGameObjGracefully();
			}
		}
		protected override Collider2D Collider2D => SelfCollider2D;
	}
}
